/**
 * @file	halPeripheral.c
 * @author	chipsea
 * @brief	
 * @version	0.1
 * @date	2020-11-30
 * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
 * @note
 */

/*********************************************************************
 * INCLUDES
 */
#include "log.h"
#include "key.h"
#include "spi.h"
#include "OSAL.h"
#include "gpio.h"
#include "flash.h"
#include "clock.h"
#include "error.h"
#include <stdio.h>
#include "pwrmgr.h"
#include "rom_sym_def.h"
#include "global_config.h"
#include "halPeripheral.h"
#include "dma.h"

uint8 Hal_TaskID;

/**
* @fn void printfFuncHex(const char *fun, int line, char *str, uint8_t *dat, int len)
* @brief print input data in hex
* @param fun: print function name
* @param line: Print line number 
* @param str: print data in string
* @param dat: print data in hex
* @param len: length of data
* @return NONE.
*/
void printfFuncHex(const char *fun, int line, char *str, uint8_t *dat, int len)
{
	LOG("%s(%d):%s:", fun, line, str);
	for (int ii = 0; ii < len; ii++)
	{
		LOG("%02X ", dat[ii]);
	}
	LOG("\n");
}


/**
* @fn void printfFuncChar(const char *fun, int line, char *str, uint8_t *dat, int len)
* @brief print input data in char
* @param fun: print function name
* @param line: Print line number 
* @param str: print data in string
* @param dat: print data in hex
* @param len: length of data
* @return NONE.
*/
void printfFuncChar(const char *fun, int line, char *str, uint8_t *dat, int len)
{
	LOG("%s(%d):%s:", fun, line, str);
	for (int ii = 0; ii < len; ii++)
	{
		LOG("%c", dat[ii]);
	}
	LOG("\n");
}

/**
* @fn void flash_test(void)
* @brief flash test function
* @param NONE
* @return NONE.
*/
void flash_test(void)
{
	char *str = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ++++1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ----1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ****1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ%%%%1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ####1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ@@@@1234567890abcdef";
    uint8_t tempStr[257] = {0};
    
    static char step = 0;
    switch(step%8)
	{
        case 0:
		{
            HalFlashErase(0x7C);
            HalFlashErase(0x7D);
            osal_memset(tempStr, 0, sizeof(tempStr));
            HalFlashRead(0x7CF80, tempStr, osal_strlen(str));
            printfFuncHex(__func__, __LINE__, "READ After Erase", tempStr, osal_strlen(str));
        }
		break;
        
		case 1:
		{
            HalFlashWrite(0x7CF80, (uint8_t *)str, (osal_strlen(str) / 2));
            HalFlashWrite(0x7D000, (uint8_t *)(str + (osal_strlen(str) / 2)), (osal_strlen(str) / 2));
            osal_memset(tempStr, 0, sizeof(tempStr));
            HalFlashRead(0x7CF80, tempStr, osal_strlen(str));
            printfFuncChar(__func__, __LINE__, "READ After Write", tempStr, osal_strlen(str));
        }
		break;
		
        case 2:
		{
            HalFlashErase(0x7C);
            HalFlashErase(0x7D);
            osal_memset(tempStr, 0, sizeof(tempStr));
            HalFlashRead(0x7CF80, tempStr, osal_strlen(str));
            printfFuncHex(__func__, __LINE__, "READ After Erase", tempStr, osal_strlen(str));
        }
		break;
		
        case 3:
		{
            LOG("%s(%d):%s:%d\n", __func__, __LINE__, "Erase Sector 0", HalFlashErase(0));
        }
		break;
		
        case 4:
		{
            LOG("%s(%d):%s:%d\n", __func__, __LINE__, "Write Overflow", HalFlashWrite(0x7CF80, (uint8_t *)str, osal_strlen(str)));
        }
		break;
		
		case 5:
		{
            HalFlashRead(0x7CF80, tempStr, osal_strlen(str));
            printfFuncChar(__func__, __LINE__, "READ After Write Overflow", tempStr, osal_strlen(str));
        }
		break;
		
		case 6:
		{
            HalFlashErase(0x7C);
            HalFlashErase(0x7D);
            osal_memset(tempStr, 0, sizeof(tempStr));
            HalFlashRead(0x7CF80, tempStr, osal_strlen(str));
            printfFuncHex(__func__, __LINE__, "READ After Erase", tempStr, osal_strlen(str));
        }
		break;
		
		case 7:
		{
            HalFlashWriteByDma(0x7CF80, (uint8_t *)str, (osal_strlen(str) / 2));
            HalFlashWriteByDma(0x7D000, (uint8_t *)(str + (osal_strlen(str) / 2)), (osal_strlen(str) / 2));
            osal_memset(tempStr, 0, sizeof(tempStr));
            HalFlashRead(0x7CF80, tempStr, osal_strlen(str));
            printfFuncChar(__func__, __LINE__, "READ After Write By Dma", tempStr, osal_strlen(str));
        }
		break;
    }
    step++;
}
void dma_cb(DMA_CH_t ch)
{
}
HAL_DMA_t dma_cfg =
{
    .dma_channel = DMA_CH_0,
    .evt_handler = dma_cb,
};
/**
 * @fn      void HalPeripheral_Init(uint8 task_id)
 * @brief   Peripheral initialization
 * @param   task_id
 * @return  none
 */
void HalPeripheral_Init(uint8 task_id)
{
	Hal_TaskID = task_id;
	
	HalDMAInit();
	osal_start_timerEx( Hal_TaskID, USR_FLASH_TEST, 502);
	HalDMAInitChannel(dma_cfg);
}

/**
 * @fn      uint16 HalPeripheral_ProcessEvent( uint8 task_id, uint16 events )
 * @brief   Peripheral event process
 * @param   task_id
 * @param	events
 * @return  none
 */
uint16 HalPeripheral_ProcessEvent( uint8 task_id, uint16 events )
{
	if(task_id != Hal_TaskID)
    {
		return 0;
	}
	
	if( events & USR_FLASH_TEST )
    {
		static uint8_t testNum = 0;
		
		flash_test();
        if(++testNum < 8)
		{
            VOID osal_start_timerEx( Hal_TaskID, USR_FLASH_TEST, 1002);
        }
        return ( events ^ USR_FLASH_TEST );
    }
	
    return 0;
}

/*********************************************************************
*********************************************************************/
